<template>
  <div class='container'>
    <el-dialog
      :title="addtitle"
      :visible.sync="addDialog"
      width="52%"
      :before-close="Close">
      <el-form :model="addArticle" :rules="rules" ref="addArticle">
        <el-form-item label="文章标题" prop="title" label-width="100px">
          <el-input placeholder="请输入文章标签" v-model="addArticle.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody" label-width="100px">
          <!-- 富文本编辑器 -->
          <quill-editor :options="editorOptions" id="editor-container" v-model="addArticle.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" label-width="100px">
          <el-input placeholder="请输入视频地址" v-model="addArticle.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="Close()">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles.js'
export default {
  props: {
    addDialog: {
      typeof: Boolean,
      default: false
    },
    addtitle: {
      typeof: String,
      required: true
    },
    currArticle: {
      typeof: Object,
      required: true
    }
  },
  data () {
    return {
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      addArticle: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['change', 'blur'] }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  methods: {
    // 关闭对话框
    Close () {
      this.addArticle = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.addArticle.resetFields()
      this.$emit('update:addDialog', false)
    },
    confirm () {
      this.$refs.addArticle.validate(async boo => {
        if (!boo) return
        if (this.addtitle === '新增文章') {
          await add(this.addArticle)
        } else {
          await update({ id: this.currArticle.id, ...this.addArticle })
        }
        this.$emit('updateList')
        this.addArticle = {
          title: '',
          articleBody: '',
          videoURL: ''
        }
        this.$refs.addArticle.resetFields()
      })
    },
    // 反显
    open () {
      if (this.currArticle.id) {
        this.addArticle.title = this.currArticle.title
        this.addArticle.articleBody = this.currArticle.articleBody
        this.addArticle.videoURL = this.currArticle.videoURL
      } else {
        this.addArticle = {
          title: '',
          articleBody: '',
          videoURL: ''
        }
      }
    }
  }
}
</script>

<style scoped lang='scss'>
  #editor-container {
    margin-bottom: 50px;
    height: 350px;
  }
</style>
